function gotoaddZone(dom,posId,element){
	var dataSet = {posId:posId,element:element};
	var dialog = editAdminDataDialog("left,top",dom,label_addZone,"/adminconsole/POSManager/POSZone/AddZone.jsp","addDialogZone",AddEventClickZone,loadElementFinishZone,dataSet);
}
function gotoeditZone(dom,posId,id){
	var dataSet = {posId:posId,id:id};
	var dialog = editAdminDataDialog("left,top",dom,label_addZone,"/adminconsole/POSManager/POSZone/AddZone.jsp","addDialogZone",AddEventClickZone,loadElementFinishZoneEdit,dataSet);
}
function loadElementFinishZone(dialog,dataSet){
	dialog.parents(".ui-dialog:first").css("width",400);
}
function loadElementFinishZoneEdit(dialog,dataSet){
	dialog.parents(".ui-dialog:first").css("width",400);
	EditZone(dialog,dataSet.id);
}

function AddEventClickZone(dialog,dataSet){
	var title = dialog.find("#title").val();
	var url = "/POSTableService";
	if(dataSet.id == null){
		var func = "addZone";
	}else{
		var func = "editZone"
	}
	var data = { func : func,
			id : dataSet.id,
			title : title,
			posId : dataSet.posId
		}
	simpleserviceWithArgs(url,data,showPOSZone,dataSet);
}

function EditZone(dialog,id){
	if(id != null){
		var url = "/POSTableService";
		var data = { func : "getZoneById",
				id : id
			}
		simpleserviceWithArgs(url,data,EditZoneSuccess,dialog);
	}
}

function EditZoneSuccess(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	dialog.find("#title").val(objArray['title']);
}

function changeStatusZone(id){
	waitingDialog({});
	var url = "/POSTableService";
	var data = { func : "editStatusPOSZone",
			zoneId : id
		}
	simpleservice(url,data,changeStatusZoneSuccess);
}

function changeStatusZoneSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#status"+objArray["id"]).text(objArray["status"]);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function DeleteZone(posId){
	waitingDialog({});
	var listCheckBox = "";
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	var url = "/POSTableService";
	var data = { func : "delZone",zoneId : listCheckBox.substring(0,listCheckBox.length - 1)}
	simpleserviceWithArgs(url,data,showPOSZone,posId);
}


function setLabelZone(addTitle,posId,back){
	var titlePos = "";
	if($(addTitle).parent().length!=0){
			var addTitle = $(addTitle).parent().parent().find("[name=titleName]").html()+" Zone";
	}else if(back!=undefined){
		var addTitle = $("#posTitle").html();
	}else{
		var addTitle = $("#titleName").html();
	}
	var listCategory = "";
	var addCategory = "";
	var sendData = "";
	var delCategory = "";
	$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
			"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
			titlePos+
			'<span id="titleName" >'+addTitle+"</span></div>");
	$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestZone+"</div>");
	$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listZone+"<span id='listsize'></span></div>");
	$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""+addZone+"\" type='button' onClick=\"gotoaddZone($(this),"+posId+");\"></span>");
	$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""+deleteZone+"\" type='button' onClick='DeleteZone("+posId+")'></span>");
}

function showPOSZone(data,posId,title,id,back){
	var objArray = new Array();
	if(data!=null&&data!="")
		objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(posId==null){	
		dataSet = posId;
		posId = dataSet.posId;
	}else{
		if(posId.posId!=null){
			dataSet = {posId:posId.posId,element:posId.element};
			posId = dataSet.posId;
			title = dataSet.element;
		}
	}
	setLabelZone(title,posId,back);
	if(id == null||id==""){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/POSTableService";
	var data = { func: "getAllPOSLimit",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer,
				posId: posId
				}
	simpleservice(link,data,showPOSZoneList);
}

function showPOSZoneList(data){
	$("[id=showList]").load("/adminconsole/POSManager/POSZone/POSZone.jsp",function(responseText, textStatus, XMLHttpRequest) {
		objArray2 = JSON.parse(data);
		if(objArray2[3]!=""){
			var sumtext = "";
			var text = "";
			var pageAmount = Math.ceil(objArray2[0]/objArray2[2]);
			var countObject = objArray2[0];
			var line = objArray2[2];
			var pagePointer = objArray2[1];
			var objArray = objArray2[3];
			$("#listsize").html("("+countObject+")");
			for(var i = 0;i< objArray.length+1;i++){
				if(i != objArray.length ){
					text = "<tr id=\"row_"+objArray[i].id+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(i+1)+". </td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i].id+"\"> </td>"+
							"<td align=\"left\" name=\"titleName\"> "+objArray[i].title+" </td>"+
							"<td align=\"center\"><span class='link' onclick=\"showPOSTable('',{posId:"+objArray[i].POSId+",zoneId:"+objArray[i].id+"},this)\" >"+objArray[i].tablesize+"Items </span>" +
							"/<span class='link' onclick=\"gotoaddCategory($(this),{'idcategory':'pospoint','posId':"+objArray[i].id+"},'add');\" > Add new </span></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"gotoeditZone($(this),"+objArray[i].POSId+","+objArray[i].id+")\">Edit</a></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"changeStatusZone("+objArray[i].id+") \"> " +
							"<span id =status"+objArray[i].id+">"+objArray[i].status+"</a>" +
							"</td>" +
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showPOSZone('',"+objArray[0].POSId+")\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showPOSZone('',"+objArray[0].POSId+")\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"30\">30</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"70\">70</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"200\">200</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
			}
			$("#showList tr").not($("#showList tr").first()).remove();
			$("#showList").append(sumtext);
			$("#pageAmount").text(pageAmount);
			if(pagePointer+1 <= pageAmount){
				$("#next").attr("onclick","showPOSZone('',\""+objArray[0].POSId+"\",'',id)");
				$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
			}
			if(pagePointer > 1){
				$("#previous").attr("onclick","showPOSZone('',\""+objArray[0].POSId+"\",'',id)");
				$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
			}
			$("[name=maxEntries]").val(line);
			for ( var i = 0; i < pageAmount; i++) {
				$("[name=selectpage]").append($("<option>").val((i+1)).html((i+1)));
			}
			$("[name=selectpage]").val(pagePointer);
		}
		closeWaitingDialog();
	});
}
